package com.thiswhyme.openfeign.gson.adapter;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description: LocalDateTime序列化器
 * @author: thiswhyme
 * @date: 2020/9/17
 */
public class LocalDateTimeSerializer implements GsonBizAdapter<LocalDateTime> {

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public Type getType() {
        return null;
    }

    @Override
    public Class getClazz() {
        return LocalDateTime.class;
    }

    @Override
    public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        return null == json ? null : LocalDateTime.parse(json.getAsString(), formatter);
    }

    @Override
    public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
        return  null == src ? null : new JsonPrimitive(formatter.format(src));
    }
}
